//
//  AlumPicCollectionViewCell.m
//  u76ho
//
//  Created by m-air-01 on 16/1/28.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "AlumPicCollectionViewCell.h"

@implementation AlumPicCollectionViewCell

- (void)awakeFromNib {
    // Initialization code
}

-(void)setModel:(PictureModel *)model
{
    for (id i in self.subviews) {
        [i removeFromSuperview];
    }
    _model = model;
    CGFloat picWidth = (SCREEN_WIDTH - SIZETO6PlusW(15)*2-SIZETO6W(6)*2)/3;
    
    UIImageView * picImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, picWidth, picWidth)];
    [self addSubview:picImgV];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, picWidth, picWidth, 20)];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLab];
    
    if ([model.pic isEqualToString:@""] || model.pic == nil) {
        [picImgV setImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
    }
    else
    {
        NSRange range = [model.pic rangeOfString:@"|"];
        if (range.location != NSNotFound) {
            NSArray * arr = [model.pic componentsSeparatedByString:@"|"];
            //        [picImgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,arr.lastObject]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
            
            [picImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,arr.lastObject]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                    picImgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                });
                
            }];
            
        }
        else
        {
            //        [picImgV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
            
            [picImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,model.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                
                
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    float tempW = (image.size.height - image.size.width) >= 0 ? image.size.width : image.size.height;
                    picImgV.image = [image croppedImageAtFrame:CGRectMake((image.size.height - image.size.width) >= 0 ? 0 : (image.size.width - image.size.height)/2, (image.size.width - image.size.height) >= 0 ? 0 : (image.size.height - image.size.width)/2 , tempW, tempW)];
                });
                
            }];
            
        }
    }
    
    
    
    titleLab.text = model.name;
    
}
-(void)setPicmodel:(PictureModel *)picmodel
{
    for (id i in self.subviews) {
        [i removeFromSuperview];
    }
    _picmodel = picmodel;
    CGFloat picWidth = (SCREEN_WIDTH - SIZETO6PlusW(15)*2-SIZETO6W(6)*2)/3;
    
    UIImageView * picImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, picWidth, picWidth)];
    [self addSubview:picImgV];
    
    UILabel * titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, picWidth, picWidth, 20)];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLab];
    
    
    NSRange range = [picmodel.pic rangeOfString:@"|"];
    if (range.location != NSNotFound) {        NSArray * arr = [picmodel.pic componentsSeparatedByString:@"|"];
        [picImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,arr.lastObject]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
        
    }
    else
    {
        [picImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",U76HOURL,picmodel.pic]] placeholderImage:[UIImage imageNamed:@"在职公司－默认动态图"]];
    }
    titleLab.text = picmodel.name;

}

-(void)setFlag:(NSString *)flag
{
    _flag = flag;
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), SIZETO6W(15), 20, 20)];
    [self addSubview:imgV];
    if ([flag isEqualToString:@"0"]) {
        [imgV setImage:[UIImage imageNamed:@"未选中22"]];
    }
    else if ([flag isEqualToString:@"1"])
    {
        [imgV setImage:[UIImage imageNamed:@"选中22"]];
    }
}

@end
